//
//  NSDictionary_DoubanAddition.m
//  CocoaBean
//
//  Created by sleeper on 3/29/10.
//  Copyright 2010 __MyCompany__. All rights reserved.
//

#import "NSDictionary_DoubanAddition.h"

NSString * const kAKEntryKeySingleProperty = @"$t";

@implementation NSDictionary (DoubanAddition)

/*
 Example:
 id =     {
	"$t" = "http://api.douban.com/miniblog/347719809";
 };
 
 (key = @"id", subkey = nil)
 
 or:
 
 category =     {
	"@scheme" = "http://www.douban.com/2007#kind";
 };
 
 (key = @"category", subkey = @"@scheme")
 
 */
- (NSString*)akPropertyForKey:(NSString*)key valueKey:(NSString *)valueKey
{
	NSString *retVal = @"";
	
	if (valueKey == nil)
		valueKey = kAKEntryKeySingleProperty;
	retVal = [[self objectForKey:key] objectForKey:valueKey];
	
	return retVal;
}

/*
 Example:
 author =     {
	name =         {
		"$t" = "\U4ed3\U592e\U5609\U63aa";
	};
 };
 
 or:
 
 author =     {
	category =     {
		 "@scheme" = "http://www.douban.com/2007#kind";
		 "@term" = "http://www.douban.com/2007#miniblog.music";
	};
 };
 
 */
- (NSString *)akPropertyForKey:(NSString *)key subkey:(NSString *)firstSubkey valueKey:(NSString *)valueKey
{
	NSString *retVal = @"";
	
	if (valueKey == nil)
		valueKey = kAKEntryKeySingleProperty;
	retVal = [[[self objectForKey:key] objectForKey:firstSubkey] objectForKey:valueKey];
	
	return retVal;
}

/*
 Example:
 "db:attribute" =     (
 {
	 "$t" = collection;
	 "@name" = category;
 },
 {
	 "$t" = listened;
	 "@name" = status;
 },
 {
	 "$t" = 3;
	 "@name" = rating;
 }
 );
 
 or:
 
 link =         (
 {
	 "@href" = "http://api.douban.com/people/2956283";
	 "@rel" = self;
 },
 {
	 "@href" = "http://www.douban.com/people/letyoudown/";
	 "@rel" = alternate;
 },
 {
	 "@href" = "http://t.douban.com/icon/u2956283-66.jpg";
	 "@rel" = icon;
 }
 );
 
 */
- (NSString*)akPropertyInArrayForKey:(NSString*)key subkey:(NSString *)firstSubkey valueKey:(NSString *)valueKey value:(NSString*)value
{
	NSString *retVal = @"";
	
	NSArray *subproperties = [self objectForKey:key];
	if (valueKey == nil)
		valueKey = kAKEntryKeySingleProperty;
	for (NSDictionary *dict in subproperties) {
		if ([[dict objectForKey:firstSubkey] isEqualToString:value]) {
			retVal = [dict objectForKey:valueKey];
			break;
		}
	}
	
	return retVal;
}

/*
 Example:
 author =	{
 "db:attribute" =     (
 {
	 "$t" = collection;
	 "@name" = category;
 },
 {
	 "$t" = listened;
	 "@name" = status;
 },
 {
	 "$t" = 3;
	 "@name" = rating;
 }
 );
 }
 
 or:
 
 author =     {
 link =         (
 {
 "@href" = "http://api.douban.com/people/2956283";
 "@rel" = self;
 },
 {
 "@href" = "http://www.douban.com/people/letyoudown/";
 "@rel" = alternate;
 },
 {
 "@href" = "http://t.douban.com/icon/u2956283-66.jpg";
 "@rel" = icon;
 }
 );
 }
 */
- (NSString*)akPropertyInArrayForKey:(NSString*)key firstSubkey:(NSString *)firstSubkey secondSubkey:(NSString *)secondSubkey valueKey:(NSString *)valueKey value:(NSString *)value
{
	NSString *retVal = @"";
	
	NSArray *subproperties = [[self objectForKey:key] objectForKey:firstSubkey];
	if (valueKey == nil)
		valueKey = kAKEntryKeySingleProperty;
	for (NSDictionary *dict in subproperties) {
		if ([[dict objectForKey:secondSubkey] isEqualToString:value]) {
			retVal = [dict objectForKey:valueKey];
			break;
		}
	} 
	
	return retVal;
}

/*
 Example:
 "db:tag" =     (
 {
 "@count" = 122;
 "@name" = "\U7535\U89c6\U5267";
 },
 {
 "@count" = 73;
 "@name" = "\U56fd\U4ea7\U7535\U89c6\U5267";
 }
 );
 */
- (void)akGetPropertyArraysForKey:(NSString *)key 
					   firstArray:(NSArray **)firstArray firstSubkey:(NSString *)firstSubkey 
					  secondArray:(NSArray **)secondArray secondSubkey:(NSString *)secondSubkey
{
	if ([key length] == 0 || firstArray == NULL || [firstSubkey length] == 0)
		return;
	
	NSArray *propertyArray = [self objectForKey:key];
	NSUInteger count = [propertyArray count];
	NSMutableArray *tempArray1 = [[NSMutableArray alloc] initWithCapacity:count];
	NSMutableArray *tempArray2 = nil;
	if (secondArray != NULL && [secondSubkey length] > 0)
		tempArray2 = [[NSMutableArray alloc] initWithCapacity:count];
	for (NSDictionary *dict in propertyArray) {
		[tempArray1 addObject:[dict objectForKey:firstSubkey]];
		if (tempArray2)
			[tempArray2 addObject:[dict objectForKey:secondSubkey]];
	}
	
	*firstArray = [[tempArray1 copy] autorelease];
	[tempArray1 release];
	
	if (tempArray2) {
		*secondArray = [[tempArray2 copy] autorelease];
		[tempArray2 release];
	}
}

/*
 Example
 "db:attribute" =     (
 {
 "$t" = "The Long Shadow Falls";
 "@index" = 1;
 "@name" = track;
 },
 {
 "$t" = "All The Pretty Little Horsies";
 "@index" = 2;
 "@name" = track;
 },
 {
 "$t" = "Calling For Vanished Faces I";
 "@index" = 3;
 "@name" = track;
 },
 );
 */
- (NSArray *)akCollectPropertyArraysForKey:(NSString *)key subkey:(NSString *)subkey value:(NSString *)value
{
	NSArray *retVal = nil;
	if ([key length] == 0 || [subkey length] == 0 || [value length] == 0)
		return retVal;
	
	NSMutableArray *tempArray = [[NSMutableArray alloc] init];
	for (NSDictionary *dict in [self objectForKey:key]) {
		if ([[dict objectForKey:subkey] isEqualToString:value])
			[tempArray addObject:dict];
	}
	
	if ([tempArray count] > 0)
		retVal = [[tempArray copy] autorelease];
	
	[tempArray release];
	
	return retVal;
}

- (NSArray *)akCollectPropertyArraysForKey:(NSString *)key subkey:(NSString *)subkey value:(NSString *)value anthoerKey:(NSString *)key2 anotherValue:(NSString *)value2
{
	NSArray *retVal = nil;
	if ([key length] == 0 || [subkey length] == 0 || [value length] == 0)
		return retVal;
	
	NSMutableArray *tempArray = [[NSMutableArray alloc] init];
	for (NSDictionary *dict in [self objectForKey:key]) {
		if ([[dict objectForKey:subkey] isEqualToString:value] && [[dict objectForKey:key2] isEqualToString:value2])
			[tempArray addObject:dict];
	}
	
	if ([tempArray count] > 0)
		retVal = [[tempArray copy] autorelease];
	
	[tempArray release];
	
	return retVal;
}

- (NSString*)dbUserId
{
	return [self akPropertyForKey:@"db:uid" valueKey:nil];
}

@end
